using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Security.Permissions;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using Org.Eurekaa.DNJ.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace Org.Eurekaa.DNJ.UI.Design.WebControls
{
    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    class DNJTabsControlDesigner : DNJMultiboxControlDesigner
    {
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            for (int i = 0; i < MultiboxControl.BoxList.Count; i++)
            {
                (MultiboxControl.Controls[0].Controls[i] as HtmlGenericControl).Attributes[DesignerRegion.DesignerRegionAttributeName] = i.ToString();
            }
            (MultiboxControl.Controls[1] as HtmlGenericControl).Attributes[DesignerRegion.DesignerRegionAttributeName] = (MultiboxControl.BoxList.Count).ToString();
        }
    }

    //[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    //class DNJTabsControlDesigner : CompositeControlDesigner
    //{
    //    #region privates
    //    private const string HEADER_PREFIX = "Header";
    //    private const string CONTENT_PREFIX = "Content";
    //    DNJTabs tabControl;
    //    #endregion privates

    //    #region implementations
    //    public override void Initialize(IComponent component)
    //    {
    //        // Initialize the base
    //        base.Initialize(component);

    //        tabControl = (DNJTabs)component;
    //    }

    //    public override bool AllowResize
    //    {
    //        get
    //        {
    //            return true;
    //        }
    //    }
        
    //    public override String GetDesignTimeHtml(DesignerRegionCollection regions)
    //    {
    //        int i = 0;

    //        //add design regions for all header cells, the region name will be prefixd with 
    //        // HEADER_PREFIX, and extended with the tab page index. 

    //        foreach (DNJBox tabPage in tabControl.TabPages)
    //        {
    //            regions.Add(new DesignerRegion(this, HEADER_PREFIX + i.ToString()));
    //            i++;
    //        }

    //        // Create an editable region and add it to the regions
    //        // the design region name will take CONTENT_PREFIX as prefix and the index
    //        // of the current active tab.

    //        EditableDesignerRegion editableRegion =
    //            new EditableDesignerRegion(this,
    //                CONTENT_PREFIX + tabControl.CurrentDesignTab, false);
    //        regions.Add(editableRegion);

    //        // Set the highlight for the selected region
    //        regions[tabControl.CurrentDesignTab].Highlight = true;

    //        // Use the base class to render the markup
    //        return base.GetDesignTimeHtml();
    //    }

    //    protected override void CreateChildControls()
    //    {
    //        base.CreateChildControls();

    //            for (int i = 0; i < tabControl.TabPages.Count; i++)
    //            {
    //               (tabControl.Controls[0].Controls[i] as HtmlGenericControl).Attributes[DesignerRegion.DesignerRegionAttributeName] = i.ToString();
    //            }

    //            //set the editable region
    //           (tabControl.Controls[1] as HtmlGenericControl).Attributes[DesignerRegion.DesignerRegionAttributeName] = (tabControl.TabPages.Count).ToString();
    //    }
    //    protected override void OnClick(DesignerRegionMouseEventArgs e)
    //    {
    //        if (e.Region == null)
    //            return;

    //        // If the clicked region is not a header, return
    //        if (e.Region.Name.IndexOf(HEADER_PREFIX) != 0)
    //            return;

    //        // Switch the current view if required
    //        //only when the clicked region is different than the active region.
    //        if (e.Region.Name.Substring(HEADER_PREFIX.Length) != tabControl.CurrentDesignTab.ToString())
    //        {
    //            //extract the index of the design region, and set the CurrentDesignTab index
    //            tabControl.CurrentDesignTab = int.Parse(e.Region.Name.Substring(HEADER_PREFIX.Length));

    //            //then after update the design time HTML
    //            base.UpdateDesignTimeHtml();
    //        }
    //    }


    //    public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
    //    {
    //        // Get a reference to the designer host
    //        IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));

    //        //only if known host, and we have some TabPages
    //        if (host != null && tabControl.TabPages.Count > 0)
    //        {
    //            //we need to get the contents of the TabBody of the tabControl

    //            ITemplate template = tabControl.TabPages[0].Content;


    //            if (region.Name.StartsWith(CONTENT_PREFIX))
    //            {
    //                //get the template of the selected tab

    //                //extract the tab index of the edited region, from the region name.
    //                int tabIndex = int.Parse(region.Name.Substring(CONTENT_PREFIX.Length));

    //                //switch the design template to the selected tab index.
    //                template = tabControl.TabPages[tabIndex].Content;
    //            }

    //            // Persist the template in the design host
    //            if (template != null)
    //                return ControlPersister.PersistTemplate(template, host);
    //        }

    //        return String.Empty;
    //    }

    //    public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
    //    {
    //        if (content == null)
    //            return;

    //        // Get a reference to the designer host
    //        IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
    //        if (host != null)
    //        {
    //            // Create a template from the content string
    //            ITemplate template = ControlParser.ParseTemplate(host, content);

    //            if (template != null)
    //            {
    //                // Determine which region should get the template
    //                if (region.Name.StartsWith(CONTENT_PREFIX))
    //                {
    //                    int tabIndex = int.Parse(region.Name.Substring(CONTENT_PREFIX.Length));
    //                    //set back the template for the selected tab body
    //                    tabControl.TabPages[tabIndex].Content = template;
    //                }
    //            }
    //        }
    //    }
    //    #endregion implementations



    //    // Create a custom ActionLists collection
    //    public override DesignerActionListCollection ActionLists
    //    {
    //        get
    //        {
    //            // Create the collection
    //            DesignerActionListCollection actionLists = new DesignerActionListCollection();

    //            // Get the base items, if any
    //            actionLists.AddRange(base.ActionLists);

    //            // Add a custom list of actions
    //            actionLists.Add(new DNJTabsActionList(this));

    //            return actionLists;

    //        }
    //    }
    //}


    //// Create an embedded DesignerActionList class
    //class DNJTabsActionList : DesignerActionList
    //{
    //    // Create private fields
    //    private DNJTabsControlDesigner _parent;
    //    private DesignerActionItemCollection items;

    //    // Constructor
    //    public DNJTabsActionList(DNJTabsControlDesigner parent)
    //        : base(parent.Component)
    //    {
    //        _parent = parent;
    //    }

    //    // Create a set of transacted callback methods
    //    // Callback for the wide format
    //    public void CreateTabPage()
    //    {
    //        DNJTabs tabView = (DNJTabs)_parent.Component;


    //        // Create the callback
    //        TransactedChangeCallback toCall = new TransactedChangeCallback(AddTabPage);
    //        // Create the transacted change in the control
    //        ControlDesigner.InvokeTransactedChange(tabView, toCall, null, "Add Tab Page");
    //    }


    //    // Get the sorted list of Action items
    //    public override DesignerActionItemCollection GetSortedActionItems()
    //    {
    //        if (items == null)
    //        {
    //            // Create the collection
    //            items = new DesignerActionItemCollection();

    //            // Add Tab Page command
    //            items.Add(new DesignerActionMethodItem(this, "CreateTabPage", "Add Tab Page", true));
    //        }
    //        return items;
    //    }

    //    // Function for the callbacks to call
    //    public bool AddTabPage(object arg)
    //    {
    //        // Get a reference to the designer's associated component
    //        DNJTabs tabsView = (DNJTabs)_parent.Component;

    //        DNJBox tp = new DNJBox();
    //        tp.ID = "DNJBox" + tabsView.TabPages.Count.ToString();
    //        tp.Title = "Tab " + tabsView.TabPages.Count.ToString(); ;
    //        tabsView.TabPages.Add(tp);

    //        _parent.UpdateDesignTimeHtml();

    //        // Return an indication of success
    //        return true;
    //    }
    //}

}
